{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 时间序列\n",
    " * 时间戳（timestamp）\n",
    " * 固定周期（period）\n",
    " * 时间间隔（interval）\n",
    "\n",
    "### date_range\n",
    " * 可以指定开始时间与周期\n",
    " * H：小时\n",
    " * D：天\n",
    " * M：月"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-07-01', '2016-07-02', '2016-07-03', '2016-07-04',\n",
       "               '2016-07-05', '2016-07-06', '2016-07-07', '2016-07-08',\n",
       "               '2016-07-09', '2016-07-10'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# TIMES # 2016 Jul 1 7/1/2016 1/7/2016 2016-07-01 2016/07/01\n",
    "rng = pd.date_range('2016/07/01', periods=10, freq='D')\n",
    "# rng = pd.date_range('2016/07/01', periods=10, freq='3D')\n",
    "# rng = pd.date_range('2016/07/01', periods=10, freq='3M')\n",
    "rng"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-01-01    0.864608\n",
      "2016-01-02   -0.120746\n",
      "2016-01-03   -2.027073\n",
      "2016-01-04   -0.302355\n",
      "2016-01-05    0.003137\n",
      "2016-01-06   -1.900082\n",
      "2016-01-07    1.793256\n",
      "2016-01-08    0.250774\n",
      "2016-01-09    0.608040\n",
      "2016-01-10   -1.878572\n",
      "2016-01-11   -0.405535\n",
      "2016-01-12    0.071926\n",
      "2016-01-13    0.489196\n",
      "2016-01-14    0.808886\n",
      "2016-01-15   -1.232750\n",
      "2016-01-16   -0.404641\n",
      "2016-01-17   -1.972161\n",
      "2016-01-18   -0.919908\n",
      "2016-01-19   -0.491904\n",
      "2016-01-20    0.516254\n",
      "Freq: D, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# freq 默认D\n",
    "rd_time = pd.Series(np.random.randn(20),\n",
    "                index=pd.date_range(pd.datetime(2016, 1, 1), periods=20))\n",
    "print(rd_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1.2327504367240143\n"
     ]
    }
   ],
   "source": [
    "print(rd_time['2016-01-15'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-01-15   -1.232750\n",
      "2016-01-16   -0.404641\n",
      "2016-01-17   -1.972161\n",
      "2016-01-18   -0.919908\n",
      "2016-01-19   -0.491904\n",
      "2016-01-20    0.516254\n",
      "Freq: D, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(rd_time['2016-01-15': '2016-01-20'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2010-01-31', '2010-02-28', '2010-03-31', '2010-04-30',\n",
      "               '2010-05-31', '2010-06-30', '2010-07-31', '2010-08-31',\n",
      "               '2010-09-30', '2010-10-31', '2010-11-30', '2010-12-31'],\n",
      "              dtype='datetime64[ns]', freq='M')\n"
     ]
    }
   ],
   "source": [
    "data = pd.date_range('2010-01-01', '2011-01-01', freq='M')\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01-10   -1.878572\n",
       "2016-01-11   -0.405535\n",
       "2016-01-12    0.071926\n",
       "2016-01-13    0.489196\n",
       "2016-01-14    0.808886\n",
       "2016-01-15   -1.232750\n",
       "2016-01-16   -0.404641\n",
       "2016-01-17   -1.972161\n",
       "2016-01-18   -0.919908\n",
       "2016-01-19   -0.491904\n",
       "2016-01-20    0.516254\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2016-01-10 之前的丢弃\n",
    "rd_time.truncate(before='2016-01-10')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01-01    0.864608\n",
       "2016-01-02   -0.120746\n",
       "2016-01-03   -2.027073\n",
       "2016-01-04   -0.302355\n",
       "2016-01-05    0.003137\n",
       "2016-01-06   -1.900082\n",
       "2016-01-07    1.793256\n",
       "2016-01-08    0.250774\n",
       "2016-01-09    0.608040\n",
       "2016-01-10   -1.878572\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2016-01-10 之后的丢弃\n",
    "rd_time.truncate(after='2016-01-10')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-01-15   -1.232750\n",
      "2016-01-16   -0.404641\n",
      "2016-01-17   -1.972161\n",
      "2016-01-18   -0.919908\n",
      "2016-01-19   -0.491904\n",
      "2016-01-20    0.516254\n",
      "Freq: D, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 切片选择\n",
    "print(rd_time['2016-01-15':'2016-01-20'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-01-10 00:00:00\n",
      "2016-01-10 10:00:00\n",
      "2016-01-10 10:15:00\n"
     ]
    }
   ],
   "source": [
    "# 时间戳\n",
    "print(pd.Timestamp('2016-01-10'))\n",
    "\n",
    "# 指定更多细节\n",
    "print(pd.Timestamp('2016-01-10 10'))\n",
    "print(pd.Timestamp('2016-01-10 10:15'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = pd.Timestamp('2016-07-10 10:15')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-01', 'M')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间区间\n",
    "pd.Period('2016-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-01-01', 'D')"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Period('2016-01-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timedelta('1 days 00:00:00')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# TIME OFFSETS\n",
    "pd.Timedelta('1 day')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-01-02 10:10', 'T')"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Period('2016-01-01 10:10') + pd.Timedelta('1 day')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-01-02 10:10:00')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('1 day')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-01-01 10:10:00.000000015')"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('15 ns')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "p1 = pd.period_range('2016-01-01 10:10', freq='25H', periods=10)\n",
    "p2 = pd.period_range('2016-01-01 10:10', freq='1D1H', periods=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2016-01-01 10:00', '2016-01-02 11:00', '2016-01-03 12:00',\n",
       "             '2016-01-04 13:00', '2016-01-05 14:00', '2016-01-06 15:00',\n",
       "             '2016-01-07 16:00', '2016-01-08 17:00', '2016-01-09 18:00',\n",
       "             '2016-01-10 19:00'],\n",
       "            dtype='period[25H]', freq='25H')"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2016-01-01 10:00', '2016-01-02 11:00', '2016-01-03 12:00',\n",
       "             '2016-01-04 13:00', '2016-01-05 14:00', '2016-01-06 15:00',\n",
       "             '2016-01-07 16:00', '2016-01-08 17:00', '2016-01-09 18:00',\n",
       "             '2016-01-10 19:00'],\n",
       "            dtype='period[25H]', freq='25H')"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-07-01    0\n",
       "2016-07-02    1\n",
       "2016-07-03    2\n",
       "2016-07-04    3\n",
       "2016-07-05    4\n",
       "2016-07-06    5\n",
       "2016-07-07    6\n",
       "2016-07-08    7\n",
       "2016-07-09    8\n",
       "2016-07-10    9\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = pd.date_range('2016 Jul 1', periods=10, freq='D')\n",
    "rng\n",
    "pd.Series(range(len(rng)), index=rng)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01   -1.684874\n",
       "2016-02   -0.112792\n",
       "2016-03    0.053951\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "periods = [pd.Period('2016-01'), pd.Period('2016-02'), pd.Period('2016-03')]\n",
    "ts = pd.Series(np.random.randn(len(periods)), index=periods)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.indexes.period.PeriodIndex"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(ts.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-07-10 08:00:00    0\n",
       "2016-07-10 09:00:00    1\n",
       "2016-07-10 10:00:00    2\n",
       "2016-07-10 11:00:00    3\n",
       "2016-07-10 12:00:00    4\n",
       "2016-07-10 13:00:00    5\n",
       "2016-07-10 14:00:00    6\n",
       "2016-07-10 15:00:00    7\n",
       "2016-07-10 16:00:00    8\n",
       "2016-07-10 17:00:00    9\n",
       "Freq: H, dtype: int64"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间戳和时间周期可以转换\n",
    "ts = pd.Series(range(10), pd.date_range('07-10-16 8:00', periods=10, freq='H'))\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-07-10 08:00    0\n",
       "2016-07-10 09:00    1\n",
       "2016-07-10 10:00    2\n",
       "2016-07-10 11:00    3\n",
       "2016-07-10 12:00    4\n",
       "2016-07-10 13:00    5\n",
       "2016-07-10 14:00    6\n",
       "2016-07-10 15:00    7\n",
       "2016-07-10 16:00    8\n",
       "2016-07-10 17:00    9\n",
       "Freq: H, dtype: int64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts_period = ts.to_period()\n",
    "ts_period"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-07-10 08:00    0\n",
       "2016-07-10 09:00    1\n",
       "2016-07-10 10:00    2\n",
       "2016-07-10 11:00    3\n",
       "Freq: H, dtype: int64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间周期包含（整点时间）\n",
    "ts_period['2016-07-10 08:30': '2016-07-10 11:45']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-07-10 09:00:00    1\n",
       "2016-07-10 10:00:00    2\n",
       "2016-07-10 11:00:00    3\n",
       "Freq: H, dtype: int64"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts['2016-07-10 08:30': '2016-07-10 11:45']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
